home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagg_m.zip / GRAPHICS.SWG / 0137_Storing 3D Graphics.pas < prev    next >
Pascal/Delphi Source File  |  1994-08-25  |  3KB  |  101 lines

  1. {
  2. From: Bschor@vms.cis.pitt.edu
  3.  
  4. > Now the problem. "Seek(F, I)" will only take ONE integer at a time!!
  5. > Naturally I need two. I'm trying to run it so that at each virtual
  6. > "square" a user can define a different message, monster, etc. And the
  7. > file i'm writing to must be able to define between X & Y, [(1,2) for
  8. > example], or both of them togeter [E.G. Two steps to the right, two steps
  9. > forward = (2,2)]. HOW DO I DO THIS???
  10.  
  11. If I understand the question correctly, you are asking how to map a
  12. two-dimensional structure (a 2-D map of your world) into a 1-dimensional
  13. data structure (a file).  Ah, my ancient Fortran knowledge does come in
  14. useful ...
  15.  
  16. The following works for arrays of any dimension, though you need to
  17. have the array size fixed.  Suppose you have dimensioned World into R rows,
  18. C columns, and L layers (I'm doing 3-D, just to show how it can be done).
  19. To make it all very clear, I'll define the world as either a 3-D or linear
  20. structure, using the Pascal Variant Record type.
  21. }
  22.  
  23. CONST
  24.  rows = 30;
  25.  columns = 40;
  26.  layers = 5;
  27.  rooms = 6000; { rows * columns * layers }
  28. TYPE
  29.  rowtype = 1 .. rows;
  30.  columntype = 1 .. columns;
  31.  layertype = 1 .. layers;
  32.  roomnumbertype = 1 .. rooms;
  33.  roomtype = RECORD
  34.  { you define as needed }
  35.  END;
  36.  worldtype = RECORD
  37.  CASE (d3, d1) of
  38.  d3 : (spatial: ARRAY [layertype, rowtype, columntype] OF roomtype);
  39.  d1 : (linear : ARRAY [roomnumbertype] OF roomtype);
  40.  END;
  41. {
  42.      Basically, you determine an order you wish to store the data.  Suppose
  43. you say "Start with the first layer, the first row, the first column.
  44. March across the columns, then move down a row and repeat across the
  45. columns; when you finish a layer, move down to the next layer and repeat".
  46.  
  47.      Clearly Layer 1, Row 1, Column C maps to Room C.  Since each row has
  48. "columns" columns, then the mapping of Layer 1, Row R, Column C is to
  49. Room (R-1)*columns + C.  The full mapping is --
  50. }
  51.   FUNCTION roomnumber (layer : layertype; row : rowtype;
  52.    column : columntype) : roomnumbertype;
  53.  
  54.   BEGIN   { roomnumber }
  55.    roomnumber := column + pred(row)*columns + pred(layer)*columns*rows
  56.   END;
  57.  
  58. {     Note you can also map in the other direction:}
  59.  
  60.   FUNCTION layer (roomnumber : roomnumbertype) : layertype;
  61.  
  62.   BEGIN   { layer }
  63.    layer := succ (pred(roomnumber) DIV (columns * rows))
  64.   END;
  65.  
  66.   FUNCTION row (roomnumber : roomnumbertype) : rowtype;
  67.  
  68.   BEGIN   { row }
  69.    row := succ ((pred(roomnumber) MOD (columns * rows)) DIV columns)
  70.   END;
  71.  
  72.   FUNCTION column (roomnumber : roomnumbertype) : columntype;
  73.  
  74.   BEGIN   { column }
  75.    column := succ (pred(roomnumber) MOD columns)
  76.   END;
  77.  
  78. {
  79.      Putting it all together, suppose you have a room, "room", with room
  80. number "roomnumber", that you want to put into the world.
  81. }
  82.  VAR world : worldtype;
  83.      room : roomtype;
  84.      roomnumber : roomnumbertype;
  85.  
  86.  WITH world DO
  87.   BEGIN
  88.    spatial[layer(roomnumber), row(roomnumber), column(roomnumber)] := room
  89.   END;
  90. {
  91.      The above fragment stores a room into the three-dimensional world.
  92. Of course, if you know the room number (which we do), you can also simply
  93. }
  94.  
  95.  WITH world DO linear[roomnumber] := room
  96. {
  97.      For the original question, note that the "roomnumber" function gives
  98. you the record number for the Seek procedure (you may need to offset by 1,
  99. depending on how Seek is implemented ...).
  100. }
  101.